package com.gtlab1207.br_security.common;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class StringUtil {

        public static String autoId(String type,String Dbid,int idSize){
            //存放最终生成的id
            String id = new String();
            //存放计算的编号尾数
            String temp = new String();

            String date = new SimpleDateFormat("yyyyMMdd").format(new Date(System.currentTimeMillis()));
            String str = type+date;
            if("".equals(Dbid)||Dbid==null){
                //如果id不存在，则设置每天的第一个id
                temp = "1";
            }else {
                //取出流水号
                int maxNum = 0;
                for(int i=0;i<=idSize;i++){
                    maxNum += 9*(10^i);
                }
                temp = Dbid.substring(Dbid.length()-idSize,Dbid.length());
                if(Integer.parseInt(temp) >= 1 && Integer.parseInt(temp)<=maxNum){
                    temp = String.valueOf(Integer.parseInt(temp)+1);
                }
            }
            Integer tempSize = temp.length();
            for(int i=0;i<idSize-tempSize;i++){
                temp ="0"+temp;
            }

            id = str+temp;
            return id;

        }

    /**
     * 将字符串转换成map
     * @param params
     * @return
     */
    public static Map<String, String> getMap(String params) {
            HashMap<String, String> map = new HashMap<>();

            int start = 0, len = params.length();

            while (start < len) {
                int i = params.indexOf(',', start);

                if (i == -1) {
                    i = params.length(); // 此时处理最后的键值对
                }

                String keyValue = params.substring(start, i);

                int j = keyValue.indexOf('=');
                String key = keyValue.substring(0, j);
                String value = keyValue.substring(j + 1, keyValue.length());

                map.put(key, value);

                if (i == params.length()) {
                    break;
                }

                start = i + 1; // index+1 为下一个键值对的起始位置
            }

            return map;
        }
}
